﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingAffairsManagementSystem.WebControl;

namespace TeachingAffairsManagementSystem.UserControl.RightContent.Admin
{
    public partial class TeacherManagement : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["DSTeacherCommand"] = null;
                if (Request.QueryString["t_id"] != null) // 通过 QueryString 查询
                {
                    TBSearch.Text = Request.QueryString["t_id"];
                    LBSearchT_ID_Click(null, null);
                }
            }

            else
            {
                // PostBack
                if (Session["DSTeacherCommand"] != null)
                {
                    DSTeacher.SelectCommand = Session["DSTeacherCommand"].ToString();
                }
            }
        }


        protected void LBSearchAll_Click(object sender, EventArgs e)
        {
            DSTeacher.SelectCommand = $"SELECT * FROM teacher ORDER BY t_id DESC";
            Session["DSTeacherCommand"] = DSTeacher.SelectCommand;
            GVTeacher.EditIndex = -1;
            GVTeacher.DataBind();
        }

        protected void LBSearchT_ID_Click(object sender, EventArgs e)
        {
            try
            {
                int t_id = Convert.ToInt32(TBSearch.Text);
                DSTeacher.SelectCommand = $"SELECT * FROM teacher WHERE t_id = {t_id} ORDER BY t_id DESC";
                Session["DSTeacherCommand"] = DSTeacher.SelectCommand;
                GVTeacher.EditIndex = -1;
                GVTeacher.DataBind();
            }
            catch
            {
                LBSearchAll_Click(null, null);
            }
        }

        protected void LBSearchUsername_OnClick(object sender, EventArgs e)
        {
            string username = TBSearch.Text;
            DSTeacher.SelectCommand = $"SELECT * FROM teacher WHERE username like '%{username}%' ORDER BY t_id DESC";
            Session["DSTeacherCommand"] = DSTeacher.SelectCommand;
            GVTeacher.EditIndex = -1;
            GVTeacher.DataBind();
        }

        protected void LBSearchEmail_OnClick(object sender, EventArgs e)
        {
            string email = TBSearch.Text;
            DSTeacher.SelectCommand = $"SELECT * FROM teacher WHERE email like '%{email}%' ORDER BY t_id DESC";
            Session["DSTeacherCommand"] = DSTeacher.SelectCommand;
            GVTeacher.EditIndex = -1;
            GVTeacher.DataBind();
        }

        protected void LBSearchS_Name_OnClick(object sender, EventArgs e)
        {
            string s_name = TBSearch.Text;
            DSTeacher.SelectCommand = $"SELECT * FROM teacher WHERE name like '%{s_name}%' ORDER BY t_id DESC";
            Session["DSTeacherCommand"] = DSTeacher.SelectCommand;
            GVTeacher.EditIndex = -1;
            GVTeacher.DataBind();
        }

        protected void GVTeacher_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.NewValues["password"] == null)
            {
                Alert.Show("密码不能为空", AlertBox.Level.Danger, true);
                e.Cancel = true;
                return;
            }

            Response.Write(Utils.IsEmail(e.NewValues["email"].ToString()));
            if (Utils.IsEmail(e.NewValues["email"].ToString()) == false)
            {
                Alert.Show("邮箱格式不正确", AlertBox.Level.Danger, true);
                e.Cancel = true;
                return;
            }
        }

        protected void GVTeacher_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            Alert.Show("修改成功", AlertBox.Level.Success, true);
        }


        protected void GVTeacher_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "LookUpLesson":
                    int commandIndex = Convert.ToInt32(e.CommandArgument);
                    string t_id = GVTeacher.Rows[commandIndex].Cells[0].Text;
                    Utils.OpenWindow($"Main.aspx?p=lm&t_id={t_id}");
                    break;
            }
        }

        protected void BtnInsert_Click(object sender, EventArgs e)
        {
            string username = TBUsername.Text;
            string password = TBPassword.Text;
            string email = TBEmail.Text;
            string name = TBName.Text;
            char gender = DLGender.Items[0].Selected ? '男' : '女';
            // 判断数据合法性
            if (username == "")
            {
                Alert.Show("用户名不能为空", AlertBox.Level.Danger, true);
                return;
            }

            if (password == "")
            {
                Alert.Show("密码不能为空", AlertBox.Level.Danger, true);
                return;
            }

            if (!Utils.IsEmail(email))
            {
                Alert.Show("邮箱格式不正确", AlertBox.Level.Danger, true);
                return;
            }

            if (name == "")
            {
                Alert.Show("姓名不能为空", AlertBox.Level.Danger, true);
                return;
            }

            // 判断用户名是否可用
            if (!Operation.IsUsernameAvailable(username))
            {
                Alert.Show("用户名已被使用", AlertBox.Level.Danger, true);
                return;
            }

            DSTeacher.InsertParameters["username"].DefaultValue = username;
            DSTeacher.InsertParameters["password"].DefaultValue = password;
            DSTeacher.InsertParameters["email"].DefaultValue = email;
            DSTeacher.InsertParameters["name"].DefaultValue = name;
            DSTeacher.InsertParameters["gender"].DefaultValue = gender.ToString();
            DSTeacher.Insert();
            GVTeacher.DataBind();
            Alert.Show("插入成功", AlertBox.Level.Success, true);
        }
    }
}